#! /usr/bin/perl

#also enable strict mode
use strict;

#use the HMTLSubs package and DBQueries package
use HTMLSubs;
use DBQueries;
use CGI;

#start by obtaining the input so that the username can be determined
my $input = obtain_input();

#ensure that input is not blank which would mean that the site is illegally accessed
if(scalar(CGI->param) == 0){
	
	#if so, redirect to login
	print CGI -> redirect("login.pl.cgi");
}

#call method to split the full string into a hash of individual variables
#this subroutine returns a reference to the submitted info hash
my $ref_submitted_info = HTMLSubs->decode_input($input);

#dereference the submitted info
my %submitted_info = %$ref_submitted_info;



#get parameters
my $username = $submitted_info{'username'};
my $file_id = $submitted_info{'fileID'};

#declare helper vars
my $storage_name;
my $file_name;

#obtain name of file
$file_name = DBQueries::getFileName($file_id);


#get the id and create the name for the system
if($file_name =~ m/txt$/){
			
	$storage_name = $file_id . "\.txt";
	
	}
	#otherwise its a pdf
	else{
			
		$storage_name = $file_id . "\.pdf";	
	}

#now use the unix bash to delete the hardcopy of the file
`rm files/$storage_name`;

#call database to remove file
DBQueries::removeFile($file_id);

#call sub to add to the log file
 my $log_text = "$ENV{'REMOTE_ADDR'} deleted $file_name from the system\.";
 HTMLSubs -> update_log($log_text);


#call sub to start html document
HTMLSubs->generate_html_beginning();
	
#call sub to print header
HTMLSubs -> generate_display_head("Document Management System", "Successfully Deleted File", $username);

print <<END_HTML;

<!--now display the message to the user-->
<h4>Please return to your profile for further action.</h4>
<br>

END_HTML

#create hash for button
my %parameter_hash;
$parameter_hash{'username'} = $username;
	
#call subroutine to display button that returns the user to the profile
HTMLSubs -> generate_html_button("profile.pl.cgi", "Go To profile", \%parameter_hash);

#now call method to produce closing html tags
HTMLSubs->generate_html_end();

#subroutine to obtain and determine what information is passed to the script
#to make this more universal, both GET and POST methods are accepted 
#this reads input for both GET and POST
sub obtain_input{

	#declare scalar to hold passed information
	my $input;

	#now retrieve the input through either get or post
	#change the case of request method
	$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;

	#if post use STDIN
	if ($ENV{'REQUEST_METHOD'} eq "POST"){
		read(STDIN, $input, $ENV{'CONTENT_LENGTH'});

		#exchange plus signs to spaces
		$input =~ s/\+/ /;

		#deal with special characters using the substitution sample provided
		$input =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;
	}
	#otherwise the env hash
	else{
		$input = $ENV{'QUERY_STRING'};
		
		#exchange plus signs to spaces
		$input =~ s/\+/ /;
		
		#deal with special characters using the substitution sample provided
		$input =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;
	}
	
	#now return the correct input
	return $input;	
}